####################################
# Replication code for Study 2
## Paper: RACE, DIVERSITY, AND THE DEVELOPMENT OF POLITICAL ATTITUDES ON COLLEGE CAMPUSES
## Journal: JOURNAL OF POLITICS
## Authors: NATHAN KAR MING CHAN & TANIKA RAYCHAUDHURI 
####################################

##################
# Load packages #
## Note run using R: Version 4.3.1 or earlier
##################

library(ggplot2)
library(ggpubr)
library(gridExtra)
library(psych)
library(readxl)
library(Rmisc)
library(stargazer)

##################
# Load data #
##################

## set working directory -- replace with file path on your device
##setwd("")

civiced_cleaned <- read.csv("study2data.csv") ## 1085 obs of 34 variables

#subset by rep course
rep.class <- subset(civiced_cleaned, subset=(rep==1))

#examine how many students taking rep course are political science majors: approx 65%
addmargins(prop.table(table(rep.class$politicalsciencemajor)))

#subset by non-rep placebo courses

#american politics students
ap.class <- subset(civiced_cleaned, subset=(americanpolitics==1))
#examine how many students taking american politics course are political science majors
addmargins(prop.table(table(ap.class$politicalsciencemajor)))

#international relations students
ir.class <- subset(civiced_cleaned, subset=(ir==1))
#examine how many students taking ir course are political science majors
addmargins(prop.table(table(ir.class$politicalsciencemajor)))

#political theory students
theory.class <- subset(civiced_cleaned, subset=(theory==1))
#examine how many students taking theory course are political science majors
addmargins(prop.table(table(theory.class$politicalsciencemajor)))


#subset data by race
white <- subset(civiced_cleaned, subset=(white==1)) #n=155
latinx <- subset(civiced_cleaned, subset=(latinx==1)) #n=335
asian <- subset(civiced_cleaned, subset=(asian==1)) #n=401

#subset data by race within rep classes
rep.class.white <- subset(rep.class, subset=(white==1)) 
rep.class.latinx <- subset(rep.class, subset=(latinx==1))  
rep.class.asian <- subset(rep.class, subset=(asian==1)) 

##################
## Main paper Table 2
##################

##CREATES TABLE 2 (code below outputs values, which were inputted on Word Tables) ###
#comparing post levels to pre levels

#mean of racial resentment post and pre course across all students
t.test(rep.class$rr.all.post, rep.class$rr.all.pre) ## p < 0.001
0.18-0.25

#mean of racial resentment post and pre course across white students only
t.test(rep.class.white$rr.all.post, rep.class.white$rr.all.pre) ## p = 0.16
0.18-0.25

#mean of racial resentment post and pre course across latinx students only ## p = 0.01
t.test(rep.class.latinx$rr.all.post, rep.class.latinx$rr.all.pre)
0.14-0.22

#mean of racial resentment post and pre course across asian students only ## p = 0.01
t.test(rep.class.asian$rr.all.post, rep.class.asian$rr.all.pre)
0.23-0.29

#mean of support for affirmative action pre and post course across all students ## p < 0.01
t.test(rep.class$affirmativeaction.post, rep.class$affirmativeaction.pre)
0.47-0.39

#mean of support for affirmative action pre and post course across white students
t.test(rep.class.white$affirmativeaction.post, rep.class.white$affirmativeaction.pre)
0.61-0.48

#mean of support for affirmative action pre and post course across latino students ## p = 0.11
t.test(rep.class.latinx$affirmativeaction.post, rep.class.latinx$affirmativeaction.pre)
0.49-0.41

#mean of support for affirmative action pre and post course across asian students ## p = 0.03
t.test(rep.class.asian$affirmativeaction.post, rep.class.asian$affirmativeaction.pre) 
0.42-0.33

##################
## Main paper Table 3
##################

##CREATES TABLE 3: (code below outputs values, which were inputted on Word Tables) ####

#Difference in mean levels of racial resentment after and before REP class 
t.test(rep.class$rr.all.post, rep.class$rr.all.pre) ## p < 0.01
0.18-0.25

#Difference in mean levels of racial resentment after and before American Politics class
t.test(ap.class$rr.all.post, ap.class$rr.all.pre) ## p = 0.19
0.33-0.31

#Difference in mean levels of racial resentment after and before International Relations class
t.test(ir.class$rr.all.post, ir.class$rr.all.pre) ## p = 0.25
0.29-0.27

#Difference in mean levels of racial resentment after and before Theory class
t.test(theory.class$rr.all.post, theory.class$rr.all.pre) ## p = 0.79
0.29-0.3

#Difference in mean in support for affirmative action after and before REP class
t.test(rep.class$affirmativeaction.post, rep.class$affirmativeaction.pre) ## p < 0.01
0.47-0.39

#Difference in mean in support for affirmative action after and before American Politics class
t.test(ap.class$affirmativeaction.post, ap.class$affirmativeaction.pre) ## p < 0.01
0.4-0.35

#Difference in mean in support for affirmative action after and before International Relations class
t.test(ir.class$affirmativeaction.post, ir.class$affirmativeaction.pre) ## p = 0.23
0.4-0.37

#Difference in mean in support for affirmative action after and before Theory class
t.test(theory.class$affirmativeaction.post, theory.class$affirmativeaction.pre) ## p= 0.08
0.41-0.35


##################
## Main paper Table 4
##################
#OLS regressions on association between 
#taking REP course on change in racial resentment; 
#REP course on change in affirmative action for full sample

moda <- lm(rr.all.panel~rep+ir+theory+
             democrat.pre+liberal.pre+
             age+female+firstgen+income+
             freshman+politicalsciencemajor+
             parental.education+gpa, data=civiced_cleaned)
summary(moda)

modb <- lm(affirmativeaction.panel~rep+ir+theory+
             democrat.pre+liberal.pre+
             age+female+firstgen+income+
             freshman+politicalsciencemajor+
             parental.education+gpa, data=civiced_cleaned)
summary(modb)

## Print Table 4:
stargazer(moda, modb,
          type="latex",
          dep.var.labels = c("Change in Racial Resentment","Change in Support for Affirmative Action"),
          covariate.labels=
            c("REP Class", "IR Class", "Theory Class"),
          omit = c("democrat.pre", "liberal.pre",
                    "age", "female", "firstgen", "income",
                     "freshman", "politicalsciencemajor",
                     "parental.education", "gpa"),
          omit.labels = c("Partisanship (Dem)",
                          "Ideology (Liberal)", "Age", "Female", "First-Gen",
                          "Income", "1st Year", "Political Science Major",
                          "Parents' Education", "GPA"),
          omit.stat=c("ser","f"),
          out="Table4.html")

##################
## Appendix Table B1
##################
## Print Table B1:
stargazer(moda, modb,
          type="latex",
          dep.var.labels = c("Change in Racial Resentment","Change in Support for Affirmative Action"),
          covariate.labels=
            c("REP Class", "IR Class", "Theory Class", "Partisanship (Dem)",
              "Ideology (Liberal)", "Age", "Female", "First-Gen",
              "Income", "1st Year", "Political Science Major",
              "Parents' Education", "GPA"),
          omit.stat=c("ser","f"),
          out="TableB1.html")

##################
## Main paper Table 5
##################

#OLS regressions on association between 
#taking REP course on change in racial resentment; 
#REP course on change in affirmative action disaggregated across race

#whites only (DV = Change in Racial Resentment)
modc <- lm(rr.all.panel~rep+ir+theory+
             democrat.pre+liberal.pre+
             age+female+firstgen+income+
             freshman+politicalsciencemajor+
             parental.education+gpa, data=white)
summary(modc)

#latinos only (DV = Change in Racial Resentment)
modd <- lm(rr.all.panel~rep+ir+theory+
             democrat.pre+liberal.pre+
             age+female+firstgen+income+
             freshman+politicalsciencemajor+
             parental.education+gpa, data=latinx)
summary(modd)

#Asians only (DV = Change in racial resentment)
modf <- lm(rr.all.panel~rep+ir+theory+
             democrat.pre+liberal.pre+
             age+female+firstgen+income+
             freshman+politicalsciencemajor+
             parental.education+gpa, data=asian)
summary(modf)

#whites only (DV=change in support for affirmative action)
modg <- lm(affirmativeaction.panel~rep+ir+theory+
             democrat.pre+liberal.pre+
             age+female+firstgen+income+
             freshman+politicalsciencemajor+
             parental.education+gpa, data=white)
summary(modg)

#latinos only (DV=change in support for affirmative action)
modh <- lm(affirmativeaction.panel~rep+ir+theory+
             democrat.pre+liberal.pre+
             age+female+firstgen+income+
             freshman+politicalsciencemajor+
             parental.education+gpa, data=latinx)
summary(modh)

#asians only (DV=change in support for affirmative action)
modi <- lm(affirmativeaction.panel~rep+ir+theory+
             democrat.pre+liberal.pre+
             age+female+firstgen+income+
             freshman+politicalsciencemajor+
             parental.education+gpa, data=asian)
summary(modi)


## Print Table 5:
stargazer(modc, modd, modf, modg, modh, modi,
          type="latex",
          dep.var.labels = c("Change in Racial Resentment","Change in Support for Affirmative Action"),
          covariate.labels=
            c("REP Class", "IR Class", "Theory Class"),
          omit = c("democrat.pre", "liberal.pre",
                   "age", "female", "firstgen", "income",
                   "freshman", "politicalsciencemajor",
                   "parental.education", "gpa"),
          omit.labels = c("Partisanship (Dem)",
                          "Ideology (Liberal)", "Age", "Female", "First-Gen",
                          "Income", "1st Year", "Political Science Major",
                          "Parents' Education", "GPA"),
          omit.stat=c("ser","f"),
          out="Table5.html")

##################
## Appendix Table B2
##################
##CREATES TABLE APPENDIX B2##
#modc (1) = Whites only; modd (2) = Latinos only; modf (3) = Asians only
stargazer(modc, modd, modf, modg, modh, modi,
          type="latex", 
          dep.var.labels = c("Change in Racial Resentment", "Change in Support for Affirmative Action"),
          covariate.labels=
            c("REP Class", "IR Class", "Theory Class", "Partisanship (Dem)",
              "Ideology (Liberal)", "Age", "Female", "First-Gen",
              "Income", "1st Year", "Political Science Major",
              "Parents' Education", "GPA"),
          omit.stat=c("ser","f"),
          out="TableB2.html")

##################
## Appendix Table B3
##################

##CREATES APPENDIX TABLE B3: (code below outputs values, which were inputted on Word Tables) ###

#impact of rep courses on racial resentment and support for affirmative action
#conditioning on political interest

#subset by students that took REP class by degree of political interest

#rep course and high interest
rep.class.high.interested <- subset(rep.class, subset=(interest.pre==1))

#rep course and moderate interest
rep.class.moderate.interested <- subset(rep.class, subset=(interest.pre==0.667))

#rep course and low interest
rep.class.low.interested <- subset(rep.class, subset=(interest.pre==0.333|interest.pre==0))

#difference in post and pre course racial resentment among high political interest students only
t.test(rep.class.high.interested$rr.all.post, rep.class.high.interested$rr.all.pre) ## p = 0.02
0.09-0.15

#difference in post and pre course racial resentment among moderate political interest students only
t.test(rep.class.moderate.interested$rr.all.post, rep.class.moderate.interested$rr.all.pre) ## p < 0.01
0.19-0.26

#difference in post and pre course racial resentment among low political interest students only
t.test(rep.class.low.interested$rr.all.pre, rep.class.low.interested$rr.all.post) ## p = 0.24
0.34-0.30

#difference in post and pre course support for affirmative action among high political interest students only
t.test(rep.class.high.interested$affirmativeaction.post, rep.class.high.interested$affirmativeaction.pre) ## p < 0.01
0.61-0.47

#difference in post and pre course support for affirmative action among moderate political interest students only
t.test(rep.class.moderate.interested$affirmativeaction.post, rep.class.moderate.interested$affirmativeaction.pre) ## p = 0.11
0.44-0.38

#difference in post and pre course support for affirmative action among low political interest students only
t.test(rep.class.low.interested$affirmativeaction.post, rep.class.low.interested$affirmativeaction.pre) ## p = 0.41
0.35-0.30

##################
## Main paper Figure 4
##################

#import data to create figure
ttestgraphsa <- read_excel("rep_ttests_grapha.xlsx")
View(ttestgraphsa)

## remove missing values from dataset
ttestgraphsa <- ttestgraphsa[c(1:2), c("variable", "mean", "ci")]
ttestgraphsa

#code to create figure 4
graph <- ggplot(ttestgraphsa, aes(y=mean, x=variable))+
  geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci), width=0.1, position=position_dodge(width=0.4))+
  geom_point(aes(shape=variable), size=2, shape=15,
             position=position_dodge(width=0.4))+
  labs(x="Dependent Variable", y="Difference in Mean Level")+
  geom_hline(yintercept=0.0, linetype="dashed", color= "black")
graph

#print figure 4
pdf("figure4.pdf", width = 7.5, height = 6)
grid.arrange(graph, nrow = 1)
dev.off()


##################
## Appendix Figure B1
##################

#import data to create figure B1
ttestgraphsb <- read_excel("rep_ttests_graphb.xlsx")
View(ttestgraphsb)

#code to create figure B1
graphb <-ggplot(ttestgraphsb, aes(x=group, y=mean, group=race))+
  geom_errorbar(aes(ymin=mean-ci, ymax=mean+ci), width=.1,position=position_dodge(width=0.4)) +
  geom_point(aes(colour=race),size=2, shape=15,
             position=position_dodge(width=0.4))+
  labs(x ="Dependent Variables", y = "Difference in Mean Level")+
  geom_hline(yintercept=0.00, linetype="dashed", color = "black") 

#print figure B1
graphb
pdf("figureB1.pdf", width = 7.5, height = 6)
grid.arrange(graphb, nrow = 1)
dev.off()